//存储用户token
import 'package:shared_preferences/shared_preferences.dart';

class Auth {
  //定义值
  String? _token;
  //定义键
  final String _key = 'token';
  //设置单例模式
  static Auth? _instance;
  //设置私有构造函数
  Auth._internal();
  //单例模式的工程构造函数
  factory Auth() {
    _instance ??= Auth._internal();
    return _instance!;
  }
  //初始化token
  Future<void> init() async {
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    _token = prefs.getString(_key);
  }

  //获取token
  String? getToken() {
    return _token;
  }

  //设置token
  Future<void> setToken(String token) async {
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setString(_key, token);
    _token = token;
  }

  ///删除token
  Future<void> removeToken() async {
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.remove(_key);
    _token = null;
  }
}
